# -*- coding: utf-8 -*-
# created on 2016/10/14

from mathsolver.functions.base import BaseFunction, BaseSetEq, BaseNumber, BaseInnerSet, BaseVariable, BaseSet
from sympy import FiniteSet, S, Range, oo


class JiHeZhenZiJiGeShu(BaseFunction):
    def solver(self, *args):
        self.label.add("真子集的个数")
        if isinstance(args[0], BaseSetEq) and isinstance(args[0].value[0], list):
            self.label.add("多个集合包含关系的应用")
            value = args[0].value["value"]
        elif isinstance(args[0], BaseInnerSet) and isinstance(args[0].value["value"], FiniteSet):
            self.label.add("多个集合包含关系的应用")
            value = list(args[0].value["value"])
        elif isinstance(args[0], BaseVariable):
            value = self.search(args[0].value)
            value = value.value["value"]
        elif isinstance(args[0], BaseSet):
            value = args[0].value
        else:
            raise Exception("to do")

        assert isinstance(value, (list, FiniteSet))
        val = 2 ** len(value)-1
        self.output.append(BaseNumber(val))
        self.steps.append(["真子集的个数为%d" % val, ""])
        return self


class JiHeGuanXi(BaseFunction):
    def solver(self, *args):
        self.label.add("判断集合间的关系")
        return self


class JiHeYuanSuGeShu(BaseFunction):
    def solver(self, *args):
        if args:
            value = args[0].value["value"]
            assert isinstance(value, (list, FiniteSet, Range))
            self.steps.append(["元素个数为%s" % len(value), ""])
            self.output.append(BaseNumber(len(value)))
        self.label.add("确定集合的元素个数")
        return self


class JiHeZiJiGeShu(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseSetEq) and isinstance(args[0].value[0], list):
            self.label.add("多个集合包含关系的应用")
            value = args[0].value[0]
        elif isinstance(args[0], BaseVariable):
            value = self.search(args[0].value)
            value = value.value["value"]
        elif isinstance(args[0], BaseSet):
            value = args[0].value
        else:
            raise Exception("to do")

        assert isinstance(value, (list, FiniteSet))
        self.output.append(BaseNumber(2 ** len(value)))
        self.steps.append(["子集的个数为%d" % 2 ** len(value), ""])
        self.label.add("子集的个数")
        return self


class JiHeBiaoShi(BaseFunction):
    def solver(self, *args):
        if "列举法" in args[1]:
            self.label.add("列举法表示集合")
        elif "描述法" in args[1]:
            self.label.add("描述法表示集合")
        else:
            raise ValueError()
        return self


class JiHeVenn(BaseFunction):
    def solver(self, *args):
        self.label.add("求出韦恩图某区域所表示的集合")
        return self


class JiHeZhengShuJi(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        self.output.append(BaseInnerSet({'var': '', 'domain': "Z", "name": name, 'value': S.Integers}))
        return self


class SetIsSet(BaseFunction):
    def solver(self, *args):
        set1, set2 = args
        if set1.value["value"] == set2.value["value"]:
            self.output.append(BaseVariable(True))
        else:
            self.output.append(BaseVariable(False))
        return self


class JiHeIsSymbolValue(BaseFunction):
    def solver(self, *args):
        name, symbol_value = args[:2]
        k, v = list(symbol_value.value.items())[0]
        self.output.append(BaseInnerSet({'var': str(k), 'domain': "R", "name": str(name.value), 'value': v}))
        return self


class SetZiRanShu(BaseFunction):
    def solver(self, *args):
        self.output.append(
            BaseInnerSet({'var': "", 'domain': "N", "name": str(args[0].value), 'value': Range(0, oo, 1)}))
        return self


if __name__ == '__main__':
    pass
